package com.sqly.doc.user;

import android.content.Context;
import android.content.Intent;
import android.graphics.Color;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.EditText;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONArray;
import com.alibaba.fastjson.JSONObject;
import com.loopj.android.http.AsyncHttpResponseHandler;
import com.loopj.android.http.RequestParams;
import com.sqly.doc.R;
import com.sqly.doc.adapter.CommonAdapter;
import com.sqly.doc.adapter.ViewHolder;
import com.sqly.doc.base.MyBaseActivity;
import com.sqly.doc.pojo.RetMsg;
import com.sqly.doc.pojo.ServicePackage;
import com.sqly.doc.pojo.ServicePackageItem;
import com.sqly.doc.util.BinderUtil;
import com.sqly.doc.util.HttpUrl;
import com.sqly.doc.util.MyToastUtil;
import com.sqly.doc.util.SharedPreferencesUtils;
import com.sqly.doc.util.UserClient;
import com.sqly.doc.view.StringUtils;
import com.sqly.doc.view.XListView;
import com.zyao89.view.zloading.ZLoadingDialog;
import com.zyao89.view.zloading.Z_TYPE;

import java.util.ArrayList;
import java.util.List;

//import cn.pedant.SweetAlert.SweetAlertDialog;
import com.cazaea.sweetalert.SweetAlertDialog;

import cz.msebera.android.httpclient.Header;

/**
 * 查看服务包项目信息
 * Created by Administrator on 2017/11/18 0018.
 */

public class servicepack_item_list extends MyBaseActivity {
    List<ServicePackageItem> list;//信息列表
    XListView listView;
    LinearLayout dd;
    ZLoadingDialog dialog;//加载动画
    Context con = servicepack_item_list.this;
    private int pageNum1 = 1;// 无关键字页数
    //状态栏
    TextView title, uname;
    LinearLayout back;
    EditText tname, total_price, dicount_price;
    Button tj;

    @Override
    protected void initUI() {
        setContentView(R.layout.servicepack_item_list);
        dd=(LinearLayout)findViewById(R.id.dd);
        listView = (XListView) findViewById(R.id.listView);
        listView.setPullLoadEnable(false);
        dialog = new ZLoadingDialog(con);
        dialog.setLoadingBuilder(Z_TYPE.DOUBLE_CIRCLE)//设置类型
                .setLoadingColor(Color.parseColor("#1C86EE"))//颜色
                .setHintText("Loading...")
                .setHintTextSize(16) // 设置字体大小 dp
                .setHintTextColor(Color.parseColor("#1C86EE"));  // 设置字体颜色
        ServicePackage sp = (ServicePackage) getIntent().getExtras().getSerializable("b");
        final String sid = sp.getSid();
        //状态栏
        title = (TextView) findViewById(R.id.title);
        back = (LinearLayout) findViewById(R.id.back);
        title.setText("服务包项目");
        back.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                finish();
            }
        });
        uname=(TextView)findViewById(R.id.uname);
        uname.setText("新增");
        uname.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View view) {
                startActivity(new Intent(con, servicepack_item_add.class).putExtra("servicepackid", sid));
            }
        });
        tname = (EditText) findViewById(R.id.tname);
        total_price = (EditText) findViewById(R.id.total_price);
        dicount_price = (EditText) findViewById(R.id.dicount_price);


        tname.setText(sp.getName());
        total_price.setText(sp.getTotal_price());
        dicount_price.setText(sp.getDiscount_price());

        tj = (Button) findViewById(R.id.tj);
        tj.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                if(null != tname && StringUtils.isEmpty(tname.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入服务包名称");
                }else if(tname.getText().toString().length()>21){
                    MyToastUtil.ShowToast(con, "服务包名称最多输入20个字符！");
                }else if(null != total_price && StringUtils.isEmpty(total_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入总价");
                }else if(!BinderUtil.isNumeric(total_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "总价必须为有效数字");
                }else if(null != dicount_price && StringUtils.isEmpty(dicount_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "请输入折扣价");
                }else if(!BinderUtil.isNumeric(dicount_price.getText().toString())){
                    MyToastUtil.ShowToast(con, "折扣价必须为有效数字");
                }else {
                    RequestParams ps = new RequestParams();
                    ps.add("entity.id",sid);

                    ps.add("entity.name", tname.getText().toString());
                    ps.add("entity.discount_price", dicount_price.getText().toString());
                    ps.add("entity.sbmac", total_price.getText().toString());
                    ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                    ps.add("type", "2");
                    UserClient.get(HttpUrl.EditServicepack, ps, new AsyncHttpResponseHandler() {
                        @Override
                        public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                            String content = new String(response);
                            RetMsg ret = JSON.parseObject(content, RetMsg.class);
                            if (ret.getCode().equals("200")) {
                                MyToastUtil.ShowToast(con, "成功");
                            }else{
                                MyToastUtil.ShowToast(con, ret.getInfo());
                            }
                        }

                        @Override
                        public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                        }
                    });
                }
            }
        });
    }

    @Override
    protected void initData() {
        if(SharedPreferencesUtils.getParam(con,"isxzgly","").equals("true")){
            uname.setVisibility(View.VISIBLE);
        }else{
            uname.setVisibility(View.GONE);
            tname.setVisibility(View.GONE);
            tj.setVisibility(View.GONE);
            dd.setVisibility(View.GONE);
        }
        getJumin();
    }

    @Override
    protected void initListener() {

    }

    public void getJumin() {
        RequestParams ps = new RequestParams();
        ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
        ps.add("type", "2");
        ps.add("page", pageNum1 + "");
        ServicePackage sp = (ServicePackage) getIntent().getExtras().getSerializable("b");
        final String sid = sp.getSid();
        ps.add("servicepackid", sid);
        UserClient.get(HttpUrl.GetSerViceItem, ps, new AsyncHttpResponseHandler() {
            @Override
            public void onStart() {
                super.onStart();
                dialog.show();
            }

            @Override
            public void onFailure(int statusCode, Header[] headers, byte[] errorResponse, Throwable e) {

                dialog.dismiss();
                MyToastUtil.ShowToast(con, "请求失败");
            }

            @Override
            public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                String content = new String(response);
                dialog.dismiss();
                RetMsg ret = JSON.parseObject(content, RetMsg.class);
                if (ret.getCode().equals("200")) {
                    if (pageNum1 == 1)
                        list = new ArrayList<ServicePackageItem>();
                    List<ServicePackageItem> ll = JSON.parseArray(
                            ret.getRd(), ServicePackageItem.class);
                    JSONArray js = JSON.parseArray(ret.getRd());
                    for (int i = 0; i < js.size(); i++) {
                        JSONObject j = js.getJSONObject(i);
                        ll.get(i).setTmid(j.getString("id"));
                    }
                    for (int i = 0; i < ll.size(); i++) {
                        list.add(ll.get(i));
                    }

                    listView.setSelection(list.size() - ll.size());
                    listView.stopRefresh();
                    listView.stopLoadMore();
                    listView.setRefreshTime("刚刚");
                    listView.setXListViewListener(new XListView.IXListViewListener() {
                        @Override
                        public void onRefresh() {
                            pageNum1 = 1;
                            getJumin();
                        }

                        @Override
                        public void onLoadMore() {
                            pageNum1 = pageNum1 + 1;
                            getJumin();
                        }
                    });
                    listView.setAdapter(new CommonAdapter<ServicePackageItem>(con, list, R.layout.servicepack_item_item) {
                        @Override
                        public void convert(ViewHolder helper, ServicePackageItem item) {
                            helper.setText(R.id.name, item.getItem_name());
                            helper.setText(R.id.member_code, item.getHis_item_code());
                            helper.setText(R.id.dj, item.getDj());
                        }
                    });
                    listView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
                        @Override
                        public void onItemClick(AdapterView<?> parent, View view, final int position, long id) {
                            if(SharedPreferencesUtils.getParam(con,"isxzgly","").equals("")){
                            }else {
                                new SweetAlertDialog(con, SweetAlertDialog.ERROR_TYPE)
                                        .setTitleText("提示")
                                        .setContentText("确定删除该服务项目么?")
                                        .setConfirmText("确定")
                                        .setCancelText("取消")
                                        .setConfirmClickListener(new SweetAlertDialog.OnSweetClickListener() {
                                            @Override
                                            public void onClick(SweetAlertDialog sweetAlertDialog) {
                                                sweetAlertDialog.dismiss();
                                                RequestParams ps = new RequestParams();
                                                ps.add("token", SharedPreferencesUtils.getParam(con, "token", "").toString());
                                                ps.add("type", "2");
                                                ps.add("id", list.get(position - 1).getTmid());
                                                UserClient.get(HttpUrl.DelServicePackageItem, ps, new AsyncHttpResponseHandler() {
                                                    @Override
                                                    public void onSuccess(int statusCode, Header[] headers, byte[] response) {
                                                        String content = new String(response);
                                                        RetMsg ret = JSON.parseObject(content, RetMsg.class);
                                                        if (ret.getCode().equals("200")) {
                                                            MyToastUtil.ShowToast(con, "成功");
                                                        }else{
                                                            MyToastUtil.ShowToast(con, ret.getInfo());
                                                        }
                                                        getJumin();
                                                    }

                                                    @Override
                                                    public void onFailure(int statusCode, Header[] headers, byte[] responseBody, Throwable error) {

                                                    }
                                                });
                                            }
                                        })
                                        .show();
                            }
                        }
                    });

                } else {
                    MyToastUtil.ShowToast(con, "获取错误");
                }

            }
        });


    }

}
